/**
 * jims
 */
package com.jims.master.modules.operation.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.operation.entity.OperationCost;
import com.jims.master.modules.operation.service.OperationCostService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 手术费用登记Controller
 * @author ZhuQ
 * @version 2017-04-12
 */
@Controller
@RequestMapping(value = "${adminPath}/operation/operationCost")
public class OperationCostController extends BaseController {

	@Autowired
	private OperationCostService operationCostService;
	
	@ModelAttribute
	public OperationCost get(@RequestParam(required=false) String id) {
		OperationCost entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = operationCostService.get(id);
		}
		if (entity == null){
			entity = new OperationCost();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(OperationCost operationCost, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OperationCost> page = operationCostService.findPage(new Page<OperationCost>(request, response), operationCost); 
		model.addAttribute("page", page);
		return "modules/operation/operationCostIndex";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"check", ""})
	public String check(OperationCost operationCost, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<OperationCost> page = operationCostService.findPage(new Page<OperationCost>(request, response), operationCost);
		model.addAttribute("page", page);
		return "modules/operation/operationCostCheck";
	}
    @RequiresPermissions("user")
    @RequestMapping(value = {"findByDrug", ""})
    public String findByDrug(OperationCost operationCost, HttpServletRequest request, HttpServletResponse response, Model model) {
//        Page<OperationCost> page = operationCostService.findByDrug(operationCost);
        List<OperationCost> operationCostList=operationCostService.findList(operationCost);
        model.addAttribute("operationCostList", operationCostList);
        return "modules/operation/operationCostIndex";
    }

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(OperationCost operationCost, Model model) {
		model.addAttribute("operationCost", operationCost);
		return "modules/operation/operationCostForm";
	}

    @RequiresPermissions("user")
    @RequestMapping(value = "createGenerate")
    public String createGenerate(OperationCost operationCost, Model model) {
        model.addAttribute("operationCost", operationCost);
        return "modules/operation/operationCostForm";
    }

    @RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(OperationCost operationCost, Model model) {
	    StringData data=new StringData();
		int i=operationCostService.save(operationCost);
        if(i>0){
            data.setCode("success");
            data.setData("保存手术费用成功");
        }else{
            data.setCode("error");
            data.setData("保存手术费用失败");
        }
        return data;
	}

    /*
    * 生成手术领药单
    * ZhuQ
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveGenerate")
    @ResponseBody
    public StringData saveGenerate(List<OperationCost> operationCostList, Model model) {
        StringData data=new StringData();
        int i=operationCostService.saveGenerate(operationCostList);
        if(i>0){
            data.setCode("success");
            data.setData("保存手术费用成功");
        }else{
            data.setCode("error");
            data.setData("保存手术费用失败");
        }
        return data;
    }


	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(OperationCost operationCost) {
	    StringData data=new StringData();
		int i=operationCostService.delete(operationCost);
        if(i>0){
            data.setCode("success");
            data.setData("删除手术费用成功");
        }else{
            data.setCode("error");
            data.setData("删除手术费用失败");
        }
		return data;
	}

    /**
     * 根据术后登记的masterId获取手术收费信息
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "findByMaster")
    public String findByMaster(OperationCost operationCost, Model model) {
        operationCostService.findByMasterId(operationCost);
        model.addAttribute("operationCost", operationCost);
        return "modules/operation/operationCostIndex";
    }

    /**
     * 根据术后登记的masterId获取手术收费信息
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "operationCostCreateList")
    public String operationCostCreateList(OperationCost operationCost, Model model) {
        List<OperationCost> operationCostList = new ArrayList<>();
        operationCost.setPerformDept(null != UserUtils.getUser().getOrgDept() ? UserUtils.getUser().getOrgDept().getId() : "");
        operationCost.updateOrgId();
        operationCost.setPrescFlag("0");
        operationCost.setItemClass("A");
        operationCost.setInoroutFlag("1");
        operationCostList = operationCostService.findByPrescFlag(operationCost);
        model.addAttribute("operationCostList", operationCostList);
        return "modules/operation/generatePrescriptionCreate";
    }

    /*
        * 点击生成手术领药单
        * ZhuQ
        * */
    @RequiresPermissions("user")
    @RequestMapping(value = "operationCostCreate")
    @ResponseBody
    public StringData operationCostCreate(OperationCost operationCost,Model model) {
        StringData data=new StringData();
        operationCost.setPerformDept(null != UserUtils.getUser().getOrgDept() ? UserUtils.getUser().getOrgDept().getId() : "");
        operationCost.updateOrgId();
        operationCost.setPrescFlag("0");
        operationCost.setItemClass("A");
        operationCost.setInoroutFlag("1");
        int i=operationCostService.operationCostCreate(operationCost);
        if(i>0){
            data.setCode("success");
            data.setData("保存手术费用成功");
        }else{
            data.setCode("error");
            data.setData("保存手术费用失败");
        }
        return data;
    }


}